package com.neusoft.nepm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.nepm.dto.AqiDistributeTotalStatisDto;
import com.neusoft.nepm.dto.AqiTrendTotalStatisDto;
import com.neusoft.nepm.dto.ProvinceItemTotalStatisDto;
import com.neusoft.nepm.mapper.GridCityMapper;
import com.neusoft.nepm.mapper.GridProvinceMapper;
import com.neusoft.nepm.mapper.StatisticsMapper;
import com.neusoft.nepm.po.Statistics;
import com.neusoft.nepm.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author zouran
 * createDate：2023/6/21  9:55
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService {
    @Resource
    private StatisticsMapper statisticsMapper;
    /********************* nepv工程 *********************/
    @Value("${global-data.city-number}")
    private Integer cityNumber;
    @Resource
    private GridCityMapper gridCityMapper;
    /********************* nepv工程 *********************/
    //获取全局数据
    @Value("${global-data.province-number}")
    private Integer provinceNumber;
    @Resource
    private GridProvinceMapper gridProvinceMapper;
    @Override
    public String getProvinceCoverage() {
        long provinceNum = gridProvinceMapper.selectCount(null);
        return String.format("%.2f",
                (provinceNum/(double)this.provinceNumber)*100);
    }
    @Override
    public String getCityCoverage() {
        long cityNum = gridCityMapper.selectCount(null);
        return String.format("%.2f",(cityNum/(double)this.cityNumber)*100);
    }
    @Override
    public List<ProvinceItemTotalStatisDto> listProvinceItemTotalStatis() {
        return statisticsMapper.listProvinceItemTotalStatis();
    }
    @Override
    public List<AqiDistributeTotalStatisDto> listAqiDistributeTotalStatis() {
        return statisticsMapper.listAqiDistributeTotalStatis();
    }
    @Override
    public List<AqiTrendTotalStatisDto> listAqiTrendTotalStatis() {
//获取当前12个月列表，作为查询参数
        List<AqiTrendTotalStatisDto> parameList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        for(int i=0;i<12;i++) {
//计算当前日期的前一个月
            calendar.add(Calendar.MONTH, -1);
            AqiTrendTotalStatisDto dto = new AqiTrendTotalStatisDto();
            dto.setMonth(sdf.format(calendar.getTime()));
            parameList.add(dto);
        }
        return statisticsMapper.listAqiTrendTotalStatis(parameList);
    }
    @Override
    public long getAqiCount() {
        return statisticsMapper.selectCount(null);
    }
    @Override
    public long getAqiGoodCount() {
        QueryWrapper<Statistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("aqi_id", 2);
        return statisticsMapper.selectCount(queryWrapper);
    }
}

